﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;   


public partial class HightScoreResults : System.Web.UI.Page
{
    public string HTMLOUT;
    public string Machine;
    public int MachineNum = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //List <PlayerHighScore> oPlayers = PlayerHighScore.GetPlayersHighScore(1, out Machine);
            Session["HS_machineNumber"] = 1;
            Session["HS_totalhighscoreMachines"] = PlayerHighScore.GetTotalHighScoreMachines();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(HightScoreResults));        

        }
        catch (Exception Ex) {
            Response.Write(Ex.Message);  
        } 
     
    }
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public string LoadData() {
        string Machine;
        MachineNum = int.Parse(Session["HS_machineNumber"].ToString());
        List<PlayerHighScore> oPlayers = PlayerHighScore.GetPlayersHighScore(MachineNum, out Machine);
        Session["HS_machineName"] = Machine;
       
        MachineNum++;
        if (MachineNum > (int)Session["HS_totalhighscoreMachines"]) MachineNum = 1;
        Session["HS_machineNumber"] = MachineNum;
        return drawTable(oPlayers);
    }



    public string drawTable(List<PlayerHighScore> oPlayerHighScore)
    {
        try
        {

            if (oPlayerHighScore.Count > 0)
            {

                StringBuilder OutPutHTML = new StringBuilder();
                //OutPutHTML.Append("<table class=\"Lotery\" style=\"width:780px;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.AppendFormat("<center><div class='Title'>{0}</div>", Session["HS_machineName"].ToString());
                OutPutHTML.Append("<table class=\"Lotery2\" style=\"text-align:left;margin-top:10px;border:1px solid black;border-collapse: collapse; padding:5px; width:400px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                //Header
                OutPutHTML.Append("<td style='text-align:center'>Rankings</td>");
                OutPutHTML.Append("<td>Jugador</td>");
                OutPutHTML.Append("<td style=\"text-align:right\">Puntuación</td>");
                
                int RowColor = 0;
                int Position = 1;
               
                foreach (PlayerHighScore iPlayerHighScore in oPlayerHighScore)
                {                    
                    //FIN CONTROL PAGINACION

                    if (RowColor % 2 == 0)
                    {
                        OutPutHTML.Append("<tr style=\"background-color:#e6e4e4\">");
                    }
                    else
                    {
                        OutPutHTML.Append("<tr style=\"background-color:#ffffff\">");
                    }

                    OutPutHTML.AppendFormat("<td style=\"text-align:center\">{0}º</td>", Position);
                    if (iPlayerHighScore.Nombre.Length > 25)
                    {
                        OutPutHTML.AppendFormat("<td><b>{0}</b></td>", iPlayerHighScore.Nombre.Substring(0, 25));
                    }
                    else {
                        OutPutHTML.AppendFormat("<td><b>{0}</b></td>", iPlayerHighScore.Nombre.ToString());
                    }
                    OutPutHTML.AppendFormat("<td style=\"text-align:right\">{0}</td>", iPlayerHighScore.Puntuacion.ToString());
                    OutPutHTML.Append("</tr>");
                    RowColor++;
                    Position++;

                }


                OutPutHTML.Append("</table></center>");
                return OutPutHTML.ToString();
            }
            else
            {

                StringBuilder OutPutHTML = new StringBuilder();
                //OutPutHTML.Append("<table class=\"Lotery\" style=\"width:780px;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.AppendFormat("<center><div class='Title'>{0}</div>", Session["HS_machineName"].ToString());
                OutPutHTML.Append("<table class=\"Lotery2\" style=\"text-align:left;margin-top:10px;border:1px solid black;border-collapse: collapse; padding:5px; width:400px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                //Header
                OutPutHTML.Append("<td style='text-align:center'>Rankings</td>");
                OutPutHTML.Append("<td>Jugador</td>");
                OutPutHTML.Append("<td style=\"text-align:right\">Puntuación</td>");      //Header
                OutPutHTML.Append("</tr>");
                OutPutHTML.Append("</table></center>");
                return OutPutHTML.ToString();
            }
        }
        catch (Exception Ex)
        {
            return "ERROR" + Ex.ToString();
        } 
    }

    
}

